package com.adcc.ags.gms.gw.service;

import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.gw.mapper.TrafficMapper;
import com.adcc.ags.gms.gw.vo.TrafficVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by xuechao on 2016/9/2.
 */
@Service("TrafficService")
public class TrafficServiceImpl implements TrafficService {

    @Autowired
    private TrafficMapper trafficMapper;

    @Autowired
    private LogFactory log;

    @Override
    public Optional<PageInfo<TrafficVO>> findAll(String rawMsg, String error_code, String dateStart, String dateEnd, String sender, String receiver, String msgType,Integer currentPage, Integer pageSize) {
        try {
            if(currentPage != null && pageSize != null){
                PageHelper.startPage(currentPage, pageSize);
            }
            if(Strings.isNullOrEmpty(rawMsg)||"undefined".equals(rawMsg)){
                rawMsg=null;
            }
            if(Strings.isNullOrEmpty(msgType)||"undefined".equals(msgType)){
                msgType=null;
            }
            if(Strings.isNullOrEmpty(dateStart)||"undefined".equals(dateStart)){
                dateStart=null;
            }
            if(Strings.isNullOrEmpty(dateEnd)||"undefined".equals(dateEnd)){
                dateEnd=null;
            }
            List<TrafficVO> list = trafficMapper.findAll(rawMsg, error_code, dateStart, dateEnd, sender, receiver, msgType);
            for (TrafficVO trafficVO : list) {
                StringBuilder sb = new StringBuilder();
                if (trafficVO.getPart1() != null && !trafficVO.getPart1().equals("")) {
                    sb.append(trafficVO.getPart1());
                }
                if (trafficVO.getPart2() != null && !trafficVO.getPart2().equals("")) {
                    sb.append(trafficVO.getPart2());
                }
                if (trafficVO.getPart3() != null && !trafficVO.getPart3().equals("")) {
                    sb.append(trafficVO.getPart3());
                }
                if (trafficVO.getPart4() != null && !trafficVO.getPart4().equals("")) {
                    sb.append(trafficVO.getPart4());
                }
                if (trafficVO.getPart5() != null && !trafficVO.getPart5().equals("")) {
                    sb.append(trafficVO.getPart5());
                }
                String strParts = Strings.nullToEmpty(sb.toString());
                trafficVO.setRawMsg(trafficVO.getRawMsg() + strParts);
                if (Strings.isNullOrEmpty(trafficVO.getError_code())){
                    trafficVO.setError_code("");
                }
                if (Strings.isNullOrEmpty(trafficVO.getReceiver())){
                    trafficVO.setReceiver("");
                }
                trafficVO.setError_code(trafficVO.getError_code().trim());
            }
            PageInfo<TrafficVO> pageInfo = new PageInfo<>(list);
            return Optional.of(pageInfo);
        } catch (NumberFormatException e) {
            log.error(TrafficService.class, "Traffic", GWLogLevel.ALARM, "findAll() error", e);
            return Optional.absent();
        }
    }

}
